package pl.alef.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Statistics;

import org.apache.commons.collections.map.HashedMap;
import org.perf4j.aop.Profiled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import pl.alef.mindplay.model.Comment;
import pl.alef.mindplay.model.Visit;
import pl.alef.mindplay.services.CommentService;
import pl.alef.mindplay.services.VisitService;

@Controller
public class CacheController {

	@Autowired
	protected CommentService commentService;
	
	protected Logger logger = LoggerFactory.getLogger(CacheController.class);
	
	List<Comment> list = new ArrayList<Comment>();
	
	@Profiled(tag="web:about:about")
	@RequestMapping("/system/cache")
	public ModelAndView index(HttpServletRequest request) {
		logger.info("welocome at about 2");
		
		ModelAndView mv = new ModelAndView("system/cache");

		CacheManager cacheManager = CacheManager.getInstance();
		
		List<Comment> comments = commentService.list();
		logger.info("comments : " + comments);
		mv.addObject("comments", comments);		
		String[] cacheNames = cacheManager.getCacheNames();
		mv.addObject("names", cacheNames);

		Map<String, Statistics> map = new HashMap<String, Statistics>();

		
		for(String cacheName : cacheNames) {
	        logger.debug(cacheName+" - "+ cacheManager.getCache(cacheName).getStatistics().toString());
	        map.put(cacheName, cacheManager.getCache(cacheName).getStatistics());
	    }		
		
		mv.addObject("cache", map);
		
		Comment comment = new Comment();
		mv.addObject("comment", comment);
		
		logger.info("go to : " + mv);
		return mv;
	}
	
	
}
